package com.wang.controller;


import com.wang.request.UserLoginRequest;
import com.wang.request.UserRegisterRequest;
import com.wang.model.UserDO;
import com.wang.service.FileService;
import com.wang.service.UserService;
import com.wang.util.JsonData;
import com.wang.enums.BizCodeEnum;
import com.wang.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 落日孤城
 * @since 2025-03-07
 */
@Api(tags = "用户管理模块")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private FileService fileService;


    /**
     * 上传用户头像
     *
     * 默认文件大小 1M,超过会报错
     *
     * @param file
     * @return
     */
    @ApiOperation("用户头像上传")
    @PostMapping(value = "upload")
    public JsonData uploadHeaderImg(
            @ApiParam(value = "文件上传",required = true)
            @RequestPart("file") MultipartFile file)
    {
        String result = fileService.uploadUserImage(file);
        return result != null?JsonData.buildSuccess(result):JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
    }

    /**
     * 用户注册
     * @param userRegisterRequest
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public JsonData register(
            @ApiParam(value = "用户注册对象")
            @RequestBody UserRegisterRequest userRegisterRequest
    ){
        JsonData register = userService.register(userRegisterRequest);
        return register;
    }

    /**
     * 用户登录
     * @param userLoginRequest
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("login")
    public JsonData login(
            @ApiParam(value = "用户登入对象")
            @RequestBody UserLoginRequest userLoginRequest
    ){
        return userService.login(userLoginRequest);
    }

    @ApiOperation("用户详情")
    @GetMapping("/detail")
    public JsonData detail(){
        UserVO user = userService.findUserDetail();
        return JsonData.buildSuccess(user);
    }
}

